<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Mshop extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('cate_model');
        $this->load->model('gift_model');
        $this->catelist = $this->cate_model->gift_cate_list(array('group'=>'currency'));
        $this->catelist = $this->cate_model->tree($this->catelist);
        $this->catelist = $this->catelist[0]['children'];
        $this->en_cate = array();
        $this->vir_cate = array();
        foreach ($this->catelist[0]['children'] as $key => $val){
            $val['url'] = site_url('mgift/en/'.$val['id']);
            $this->en_cate[$key] = $val;
            
        }
        foreach ($this->catelist[1]['children'] as $key =>  $val){
            $val['url'] = site_url('mgift/vir/'.$val['id']);
            $this->vir_cate[$key] = $val;
        }
        $this->assign("module", "mshop");
    }
    /**
     * 兑换商品首页
     * @param type $type 实物/虚拟
     * @param type $gift_type 商品类型
     */
    function index($type,$gift_type) {
        $where = array();
        $type = $type ? $type : '';
        $gift_type = $gift_type ? $gift_type : '';
        $records_per_page = 12;
        $type_name = '';
        $cate = array();
        if($type == 'en'){
            $cate = $this->en_cate;
            $where['type'] = 3;
            $tips = "en";
        }elseif($type == 'vir'){
            $cate = $this->vir_cate;
            $where['type'] = 4;
            $type_name = "虚拟充值";
            $tips = "vir";
        }else{
            foreach ($this->catelist[0]['children'] as $ca){
                array_push($cate, $ca);
            }
            foreach ($this->catelist[1]['children'] as $ca){
                array_push($cate, $ca);
            }
            foreach ($cate as $key=>$val){
                $tip = $val['pid'] == 3 ? "en" : 'vir';
                $cate[$key]['url'] = site_url('mshop/'.$tip.'/'.$val['id']);
            }
            $type_name = "全部";
            $t_url = site_url('mshop');
            
        }
        if($gift_type){
            $where['cate_id'] = $gift_type;
            $result = $this->db->select("title")->from("gift_category")->where("id",$gift_type)->get()->row_array();
            $type_name = $result['title'];
            if($gift_type == 15){
                $tips = 15; 
            }else if($gift_type == 14){
                $tips = 14; 
            }elseif ($gift_type == 7){
                $tips = 7; 
            }
        }
        
        
        
        $where['status'] = 1;
        $getCount = $this->gift_model->get_count($where);
        $page = new Paging($getCount,$records_per_page);
        $list = $this->gift_model->get_list($where,(($page->get_page() - 1) * $records_per_page),$records_per_page);
        foreach ($list as $key => $val){
            $list[$key]['thumb'] = strstr($val['thumb'], "http://") ? $val['thumb'] : $this->img_url.$val['thumb'];
            $tip = $val['type'] == 3 ? "en" : 'vir';
            $list[$key]['amount'] = number_format($val['amount']);
            $list[$key]['url'] = site_url('mdetail/'.$tip.'/'.$val['id']);
        }
        $this->assign('list', $list);
        $this->assign('type_name', $type_name);
        $this->assign('showPage', $page->showpage());
        $this->assign('gift_type', $gift_type);
        $this->assign('type', $type);
        $this->assign('tips', $tips);
        $this->assign('t_url', $t_url);
        $this->display('mshop/index.html');
    }
    
    function detail($type,$id){
        $type = $type ? $type : '';
        $id ? '' : redirect('mshop');
        $item = $this->gift_model->get($id);
        $item ? '' : redirect('mshop');
        if($this->userinfo){
            $need = $this->user_info['balance'] - $item['amount'];
            $is_big = $need >= 0 ? 1 : 0;
            $need = $need >= 0 ? $need : -$need;//所需金额
            $this->assign('need',$need);
            $this->assign('is_big',$is_big);
        }
        $item['thumb'] = strstr($item['thumb'], "http://") ? $item['thumb'] : $this->img_url.$item['thumb'];
        $item['amount'] = number_format($item['amount']);
        $item['description'] = strstr($item['description'],"http://") ? $item['description'] : str_replace('<img src="', '<img src="'.$this->img_url, $item['description']);
        //获取默认地址
        //收货地址
        $address_list = $this->users_model->get_shipping_address_list(array("user_id"=>$this->user_info['user_id'],'is_show'=>1,"is_default"=>1));
        $this->assign('address_default', $address_list[0] ? json_encode($address_list[0]) : json_encode(array("name"=>"","mobile"=>"","address"=>"")));
        $this->assign('action', 'mshop');
        $this->assign('item', $item);
        $this->assign('type', $type);
        $this->assign('gift_type', $item['cate_id']);
        $this->display('mshop/detail.html');
    }
}
